package com.ruoyi.web.controller.devops;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.devops.domain.DevOpsResourcesInfo;
import com.ruoyi.devops.service.IDevOpsEnvInfoService;
import com.ruoyi.devops.service.IDevOpsResourcesInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 资源配置Controller
 *
 * @author ruoyi
 * @date 2023-01-26
 */
@Controller
@RequestMapping("/devops/resources")
public class DevOpsResourcesInfoController extends BaseController {
    private String prefix = "devops/resources";

    @Autowired
    private IDevOpsResourcesInfoService devOpsResourcesInfoService;
    @Autowired
    private IDevOpsEnvInfoService devOpsEnvInfoService;

    @RequiresPermissions("devops:resources:view")
    @GetMapping()
    public String resources() {
        return prefix + "/resources";
    }

    /**
     * 查询资源配置列表
     */
    @RequiresPermissions("devops:resources:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DevOpsResourcesInfo devOpsResourcesInfo) {
        startPage();
        List<DevOpsResourcesInfo> list = devOpsResourcesInfoService.selectDevOpsResourcesInfoList(devOpsResourcesInfo);
        return getDataTable(list);
    }

    /**
     * 导出资源配置列表
     */
    @RequiresPermissions("devops:resources:export")
    @Log(title = "资源配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DevOpsResourcesInfo devOpsResourcesInfo) {
        List<DevOpsResourcesInfo> list = devOpsResourcesInfoService.selectDevOpsResourcesInfoList(devOpsResourcesInfo);
        ExcelUtil<DevOpsResourcesInfo> util = new ExcelUtil<DevOpsResourcesInfo>(DevOpsResourcesInfo.class);
        return util.exportExcel(list, "资源配置数据");
    }

    /**
     * 新增资源配置
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        modelMap.put("envList", devOpsEnvInfoService.selectDevOpsEnvInfoList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存资源配置
     */
    @RequiresPermissions("devops:resources:add")
    @Log(title = "资源配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DevOpsResourcesInfo devOpsResourcesInfo) {
        devOpsResourcesInfo.setUuid(UUID.randomUUID().toString());
        devOpsResourcesInfo.setStatus("1");
        return toAjax(devOpsResourcesInfoService.insertDevOpsResourcesInfo(devOpsResourcesInfo));
    }

    /**
     * 修改资源配置
     */
    @RequiresPermissions("devops:resources:edit")
    @GetMapping("/edit/{uuid}")
    public String edit(@PathVariable("uuid") String uuid, ModelMap mmap) {
        DevOpsResourcesInfo devOpsResourcesInfo = devOpsResourcesInfoService.selectDevOpsResourcesInfoByUuid(uuid);
        mmap.put("devOpsResourcesInfo", devOpsResourcesInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存资源配置
     */
    @RequiresPermissions("devops:resources:edit")
    @Log(title = "资源配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DevOpsResourcesInfo devOpsResourcesInfo) {
        return toAjax(devOpsResourcesInfoService.updateDevOpsResourcesInfo(devOpsResourcesInfo));
    }

    /**
     * 删除资源配置
     */
    @RequiresPermissions("devops:resources:remove")
    @Log(title = "资源配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(devOpsResourcesInfoService.deleteDevOpsResourcesInfoByUuids(ids));
    }

    /**
     * 批量添加 资源配置   环境编码|IP|用户名|密码|备注
     */
    @RequiresPermissions("devops:resources:batAdd")
    @Log(title = "批量导入", businessType = BusinessType.INSERT)
    @PostMapping("/batAdd")
    @ResponseBody
    public AjaxResult batAdd(String info) {
        logger.info(info);

        String[] infos = info.split("\n");
        for (String s : infos) {
            String[] split = s.split("\\|");
            if (split.length != 5) break;

            DevOpsResourcesInfo devOpsResourcesInfo = new DevOpsResourcesInfo();
            devOpsResourcesInfo.setUuid(UUID.randomUUID().toString());
            devOpsResourcesInfo.setResenvcode(split[0]);
            devOpsResourcesInfo.setReshost(split[1]);
            devOpsResourcesInfo.setRespassword(split[3]);
            devOpsResourcesInfo.setResport("22");
            devOpsResourcesInfo.setResusername(split[2]);
            devOpsResourcesInfo.setStatus("1");
            devOpsResourcesInfo.setSearchValue("");
            devOpsResourcesInfo.setCreateBy("");
            devOpsResourcesInfo.setCreateTime(new Date());
            devOpsResourcesInfo.setUpdateBy("");
            devOpsResourcesInfo.setUpdateTime(new Date());
            devOpsResourcesInfo.setRemark(split[4]);
            devOpsResourcesInfoService.insertDevOpsResourcesInfo(devOpsResourcesInfo);
        }

        return null;
    }
}
